#include"iostream"
#include"list"
using namespace std;
#include<stdio.h>
#include <conio.h>
#include "TictactoeLib.h"
typedef list<char>Mylist;
static list<char> s_lGlobalDataWite;
static list<char> s_lGlobalDataRead;

void SwitchPlayer()
{
	int localMode;
	while(true)
	{
		printf("\n SELECT YOUR MODE (1 -PLAY GAME, 2 - REPLAY GAME, OTHERS - EXIT GAME):");
		scanf("%d", &localMode);
		switch(localMode)
		{
		case 1 : InputNamePlayer();
				ShowBoard();
				InGame();
				break;
		case 2 :
				ReplayGame();
				break;
		default : exit (EXIT_FAILURE);break;
		}
	}
}
void ReplayGame()
{	 
	int m = 0;
	ReadRMS();
	for (Mylist::const_iterator i = s_lGlobalDataRead.begin(); i != s_lGlobalDataRead.end(); ++i)
	{
		if(m%s_iGlobalSizeBoard == 0)
		{
			printf("\n");
		}
		if(m % (s_iGlobalSizeBoard*s_iGlobalSizeBoard) == 0)
		{
			printf("\n Current Step: %d\n", m/((s_iGlobalSizeBoard*s_iGlobalSizeBoard)));
		}
		if(*i == 'O')
		{
			printf(" O");
		}
		else if(*i == 'X')
		{
			printf(" X");
		}
		else if(*i == 'T')
		{
			printf(" T");
		}
	m++;
	}
	printf("\n");
	getch();
}
void ReadRMS()
{
    FILE * pFile;
	pFile = fopen("save.bin","r+");
	char tmp;
 
	 while(true)
	{
		
	 fscanf(pFile, "%c", &tmp);
	//fread(tmp, 3, 1, pFile);
	 s_lGlobalDataRead.push_back(tmp);
	 if(feof(pFile))
	 break;	 
	 
	}

    fclose(pFile);
}
void SaveRMS()
{
    FILE * pFile;
	pFile = fopen ("save.bin","w+");
 
	for (Mylist::const_iterator i = s_lGlobalDataWite.begin(); i != s_lGlobalDataWite.end(); ++i)
	{
	 fprintf (pFile, "%c", *i);
	}
 
    fclose (pFile);
    printf("\nThe game was saved !!!\n");
}
void SaveCurrentState()
{
	for(int i =0; i < s_iGlobalSizeBoard; i++ )
		for(int j =0 ; j < s_iGlobalSizeBoard; j ++)
		{
			s_lGlobalDataWite.push_back(s_cGlobalBoard[i][j]);
		}
}
void ExitOrReplayGame()
{
	int localMode;
	while(true)
	{
		printf("\n(1 -TRY AGAIN, 2 - SAVE AND CONTINUES, OTHERS - EXIT GAME):");
		scanf("%d", &localMode);
		switch(localMode)
		{
		case 1 : SwitchPlayer();
				break;
		case 2 :SaveRMS();
				SwitchPlayer();
				break;
		default : exit (EXIT_FAILURE);break;
		}
	}
}
void InGame()
{
	char* localPosition = new char[];
	while(true)
	{
		cout << "\n Your turn "<<(s_bGlobalSwithPlayer ? s_cGlobalPlayer1 : s_cGlobalPlayer2)<<(s_bGlobalSwithPlayer ?" O:" : " X:" );
		cin >> localPosition;
		int i = localPosition[0] - '0';
		int j = localPosition[1] - '0';
		if(strlen(localPosition)>=3 || i >= (s_iGlobalSizeBoard)*(s_iGlobalSizeBoard) || j >= (s_iGlobalSizeBoard)*(s_iGlobalSizeBoard) || strlen(localPosition)<=1 )
		{
			cout<< "\n You went out the board";
			
			
		}
		else
		{
			i = i -1;
			j = j -1;
			if(s_bGlobalSwithPlayer)
			{
				if(s_cGlobalBoard[i][j] != 'O' && s_cGlobalBoard[i][j] != 'X')
				{
					s_cGlobalBoard[i][j] = 'O';
					SaveCurrentState();
					s_bGlobalSwithPlayer = !s_bGlobalSwithPlayer;
				}
				else 
				{
					cout << "This move invalid!\n";

				}
				if(IsCheckWon('O'))
				{
					cout << "Congratulation "<<(!s_bGlobalSwithPlayer ? s_cGlobalPlayer1 : s_cGlobalPlayer2);
					cout << "\nYou won!!!\n";
					ExitOrReplayGame();
					break;
				}	
			}
			else
			{
				if(s_cGlobalBoard[i][j] != 'O' && s_cGlobalBoard[i][j] != 'X')
				{
					s_cGlobalBoard[i][j]  = 'X';
					SaveCurrentState();
					s_bGlobalSwithPlayer = !s_bGlobalSwithPlayer;
				}
				else 
				{
					cout << "This move invalid!\n";
				}
				if(IsCheckWon('X'))
				{
					cout << "Congratulation "<<(!s_bGlobalSwithPlayer ? s_cGlobalPlayer1 : s_cGlobalPlayer2);
					cout << "\nYou won!!!\n";
					ExitOrReplayGame();
					break;
				}
			}
			ShowBoard();
			

		}
	}

}

void ShowBoard()
{
	for(int i = 0 ; i < s_iGlobalSizeBoard ; i ++ )
	{
		for(int j = 0; j < s_iGlobalSizeBoard; j ++)
			cout << s_cGlobalBoard[i][j]<<" ";
		cout<<"\n";
	}
}
void InputNamePlayer()
{
	cout << "Enter Player 1 Name:";
	cin >> s_cGlobalPlayer1 ;
	cout << "Enter Player 2 Name:";
	cin >> s_cGlobalPlayer2 ;
	//printf("s_cGlobalPlayer1=%x",&s_cGlobalPlayer1);
	cout << "Name1="<<s_cGlobalPlayer1<<endl;
	cout << "Name2="<<s_cGlobalPlayer2<<endl;
	
}
bool IsCheckWon(char a)
{
	if(CheckRow(a)||CheckColumn(a)||CheckCrossIncrease(a))
	return true;
	else return false;

}
int CheckRow(char a)
{
	int localCount = 0;
	int localResult = 0;
	for(int i = 0; i < s_iGlobalSizeBoard; i++)
	{
		for(int j = 0; j < s_iGlobalSizeBoard; j++)
			if(s_cGlobalBoard[i][j] == a)
			{
				if(s_cGlobalBoard[i][j+1] == a)
				{
					localCount += 1;
				}
			}
			if(localCount == 2)
			{
				localResult = 1;
				break;
			}
			else localResult = 0;
	}
	return localResult;
}
int CheckColumn(char a)
{
	int localCount = 0;
	int localResult = 0;
	for(int i = 0; i < s_iGlobalSizeBoard; i++)
	{
		for(int j = 0; j < s_iGlobalSizeBoard; j++)
			if(s_cGlobalBoard[j][i] == a)
			{
				if(s_cGlobalBoard[j+1][i] == a)
				{
					localCount += 1;
				}
			}
			if(localCount == 2)
			{
				localResult = 1;
				break;
			}
			else localResult = 0;
	}
	return localResult;

}
int CheckCrossIncrease(char a)
{
	int localResult = 0;
	int localCount = 0;
	for(int i = 0; i < s_iGlobalSizeBoard; i++)
	{
		if(s_cGlobalBoard[i][s_iGlobalSizeBoard -i - 1] == a )
			if(s_cGlobalBoard[i][s_iGlobalSizeBoard - 1 - i ] == a )
			localCount++;
		if(localCount == 3)
		{
			localResult = 1;
			break;
		}
		else localResult = 0;
	}
	localCount = 0;
	for(int i = 0; i < s_iGlobalSizeBoard; i++)
	{
		if(s_cGlobalBoard[i][i] == a)
			if(s_cGlobalBoard[i+1][i+1] == a)
				localCount++;
		if(localCount == 3)
		{
			localResult = 1;
			break;
		}
		else localResult = 0;

	}
	return localResult;


}
void Tictactoe()
{

	printf("Welcome To Tic-tac-toe game! Play with your way!");
	printf("\n");
	printf("\n");
	printf("\n");
	printf("If you find any problem, please contact tho.lekhac@gameloft.com\n");
	while(true)
	{
		printf("Input size board: ");
		scanf("%d",&s_iGlobalSizeBoard);
		if(s_iGlobalSizeBoard == 3)
			break;
		else printf("Board's size invail: ");
	}
	for(int i = 0 ; i < s_iGlobalSizeBoard ; i ++ )
		for(int j = 0; j < s_iGlobalSizeBoard; j ++)
			s_cGlobalBoard[i][j] = 'T';
	s_bGlobalSwithPlayer = true;
	
	s_cGlobalPlayer1 = new char[];
	s_cGlobalPlayer2 = new char[];
	

}